
import { defineStore } from "pinia";
import { ref } from "vue";

const useGlobalStore = defineStore('global',()=>{
    //数据
    const isCollpse = ref(false) //首页菜单是否折叠
    //面包屑的title
    const title = ref('')
    //用户信息
    const userInfo = ref({})
    //删除的权限
    const deleteAuthority = ref(false)
    //token令牌
    const token = ref('')
    //进入系统的权限，如果是管理员就让他进入系统，如果不是管理员就不让进
    const adminAuthority = ref(false)
    //方法
    const changeCollpse = ()=>{
        isCollpse.value = !isCollpse.value
    }
    const changeTitle = (newValue:string)=>{
        title.value = newValue
    }
    const getUserInfo = () => {
        return userInfo.value
    }
    const setUserInfo = (val:Object) => {
        userInfo.value = val
        updateDeleteAuthority(val)
        updateAdminAuthority(val)
    }
    const clearUserInfo = () => {
        userInfo.value = {}
        deleteAuthority.value = false
    }
    const updateDeleteAuthority = (userInfo: any) => { 
        if (userInfo && userInfo.is_super == 1) {
            deleteAuthority.value = true; 
        }
    }
    const setToken = (val:any) => {
        token.value = val
    }
    const updateToken = (val:any) => {
        token.value = val
    }
    const clearToken = () => {
        token.value = ''
    }
    const updateAdminAuthority = (userInfo: any) => { 
        if (userInfo && userInfo.role == 1) {
            adminAuthority.value = true; 
        }else{
            adminAuthority.value = false;
        }
    }
    return{
        isCollpse,
        title,
        userInfo,
        deleteAuthority,
        token,
        adminAuthority,
        changeCollpse,
        changeTitle,
        getUserInfo,
        setUserInfo,
        clearUserInfo,
        setToken,
        updateToken,
        clearToken,
        updateAdminAuthority
    }
},{persist:true})

export default useGlobalStore
